package com.djhu.freepartner.web.control;


import com.djhu.freepartner.bean.PartnerUser;
import com.djhu.freepartner.bean.UserStatus;
import com.djhu.freepartner.core.RestRsp;
import com.djhu.freepartner.core.Status;
import com.djhu.freepartner.core.exception.ServiceException;
import com.djhu.freepartner.service.IPartnerUserService;
import com.djhu.freepartner.utils.RegUtils;
import com.djhu.freepartner.web.service.BonuspointsService;
import com.djhu.freepartner.web.service.UserService;
import com.djhu.freepartner.web.service.model.UserValidModel;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/user")
@Slf4j
public class UserControl {


    @Autowired
    private IPartnerUserService partnerUserService;

    @Autowired
    private UserService userService;

    @Autowired
    private BonuspointsService bonuspointsService;



    @ApiOperation(value="用户修改头像", notes="用户修改头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonObject", value = "id以及头像url", required = true, dataType = "JSONObject")
    })
    @RequestMapping(value = "/updateportrait", method = RequestMethod.POST)
    public RestRsp updateportrait(@RequestBody JSONObject jsonObject) throws ServiceException {
        String userid = jsonObject.getString("userid");
        String portrait = jsonObject.getString("portrait");
        log.info("updateEmail receive userid ,userid is {},portrait is {}",userid,portrait);
        PartnerUser user = new PartnerUser();
        user.setUserid(userid);
        user.setPortrait(portrait);
        RestRsp restRsp = new RestRsp();
        if (partnerUserService.updateById(user)){
            restRsp.setCode(Status.SUCCESS.getCode());
            restRsp.setMsg("头像修改成功!!!");
            return restRsp;
        }else {
            throw new ServiceException("用户修改头像，请稍微再试",Status.SERVER_ERROR.getCode());
        }
    }

    @ApiOperation(value="用户修改邮箱账号", notes="用户修改邮箱账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonObject", value = "id以及用户信息", required = true, dataType = "JSONObject")
    })
    @RequestMapping(value = "/updatemail", method = RequestMethod.POST)
    public RestRsp updateEmail(@RequestBody JSONObject jsonObject) throws ServiceException {
        String userid = jsonObject.getString("userid");
        String email = jsonObject.getString("email");
        log.info("updateEmail receive email ,userid is {},email is {}",email,userid);
        boolean match = RegUtils.match(email,RegUtils.REG_EMAIL);
        if (!match){
            throw new ServiceException(Status.EMAIL_NOT_VERIFY);
        }
        PartnerUser user = new PartnerUser();
        user.setUserid(userid);
        user.setEmail(email);
        RestRsp restRsp = new RestRsp();
        if (partnerUserService.updateById(user)){
            restRsp.setCode(Status.SUCCESS.getCode());
            restRsp.setMsg("邮箱修改成功!!!");
            return restRsp;
        }else {
            throw new ServiceException("邮箱修改失败，请稍微再试",Status.SERVER_ERROR.getCode());
        }
    }


    @ApiOperation(value="用户修改注册手机号", notes="用户修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonObject", value = "用户实体user", required = true, dataType = "JSONObject")
    })
    @RequestMapping(value = "/updatephone", method = RequestMethod.POST)
    public RestRsp updatePhone(@RequestBody JSONObject jsonObject) throws ServiceException{
        String userid = jsonObject.getString("userid");
        String phone = jsonObject.getString("phone");
        log.info("updatePhone receive phone ,userid is {},phone is {}",userid,phone);
        boolean match = RegUtils.match(phone,RegUtils.REG_MOBILE);
        if (!match){
            log.error("手机号验证失败,phone is {},userid is {}",phone,userid);
            throw new ServiceException(Status.BAD_PARAM);
        }
        PartnerUser user = new PartnerUser();
        user.setUserid(userid);
        user.setPhone(phone);
        RestRsp restRsp = new RestRsp();
        if (partnerUserService.updateById(user)){
            restRsp.setCode(Status.SUCCESS.getCode());
            restRsp.setMsg("电话修改成功!!!");
            return restRsp;
        }else {
            throw new ServiceException("电话修改失败，请稍微再试",Status.SERVER_ERROR.getCode());
        }
    }


    @ApiOperation(value="实名认证", notes="实名认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonObject", value = "用户实体user", required = true, dataType = "JSONObject")
    })
    @RequestMapping(value = "/uservalid", method = RequestMethod.POST)
    public RestRsp uservalid(@RequestBody JSONObject jsonObject) throws ServiceException {
        String userid = jsonObject.getString("userid");
        String name = jsonObject.getString("name");
        String idcard = jsonObject.getString("idcard");
        log.info("用户验证接口 ,接受到 userid is {},name is {}, idcard is {}",userid,name,idcard);
        if (StringUtils.isAnyEmpty(userid,name,idcard)){
            throw new ServiceException(Status.BAD_PARAM);
        }
        UserValidModel validModel = new UserValidModel(userid,idcard,name);
        RestRsp rest = userService.validstatus(validModel);
        if (rest.getCode() == Status.SUCCESS.getCode()){
            //修改用户状态
            //增加用户积分
            rest = userService.valid(userid,idcard,name);
            return rest;
        }else {
            return rest;
        }
    }


    @ApiOperation(value="查看用户信息", notes="查看用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonObject", value = "用户的ID", required = true, dataType = "JSONObject")
    })
    @RequestMapping(value = "/userinfo", method = RequestMethod.POST)
    public RestRsp userinfo( @RequestBody JSONObject jsonObject) throws ServiceException {
        String userId = jsonObject.getString("userid");
        PartnerUser user = partnerUserService.selectById(userId);
        if (user == null){
            throw new ServiceException(Status.ACCOUNT_NOT_FOUND);
        }
        PartnerUser retUser = new PartnerUser();
        retUser.setIdcardverifi(user.getIdcardverifi());
        retUser.setUserid(user.getUserid());
        retUser.setEmail(user.getEmail());
        retUser.setPhone(user.getPhone());
        retUser.setPortrait(user.getPortrait());
        retUser.setUsername(user.getUsername());
        RestRsp restRsp = new RestRsp();
        restRsp.setCode(Status.SUCCESS.getCode());
        restRsp.setData(retUser);
        return restRsp;
    }


    @ApiOperation(value="删除用户", notes="删除用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonObject", value = "用户的ID", required = true, dataType = "String")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public RestRsp delete( @RequestBody JSONObject jsonObject){
        String userid = jsonObject.getString("userid");
        log.info("准备删除用户，{} ",userid);
        PartnerUser partnerUser = new PartnerUser();
        partnerUser.setUserid(userid);
        partnerUser.setStatus(String.valueOf(UserStatus.DELETE.getCode()));
        partnerUserService.updateById(partnerUser);
        RestRsp restRsp = new RestRsp();
        restRsp.setCode(Status.SUCCESS.getCode());
        return restRsp;
    }




}
